package org.elephantt.webby;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * A runtime exception that represents a non-2xx class HTTP problem. This class implements Responder so that it can
 * send respond to the HTTP request with the configured HTTP status code and message.
 *
 * Applications can thus throw an instance of this class at any point in their flow and have the appropriate HTTP status
 * sent. Common HTTP status codes (such as 404 Not Found) would be convenient as subclasses.
 */
public class HttpException extends RuntimeException {
  private static final long serialVersionUID = -7409985717232291749L;

  private final int code;
  private final String message;

  public HttpException(int code, String message) {
    this.code = code;
    this.message = message;
  }

  public HttpException(String message) {
    this(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, message);
  }

  public HttpException(int code) {
    this.code = code;
    this.message = null;
  }

  public int getCode() {
    return code;
  }

  public String getMessage() {
    return message;
  }
}